/**
 * 创建时间： 2017年5月9日
 *
 * @Copyright 武汉雷软科技有限公司
 */
package com.lrkj.framework.util.socket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lrkj.framework.util.hex.MacUtil;

/**
 * <pre>
 * 创建人： 郭亮
 * 类描述：
 * 版本号：0.0.1
 * 修改人：
 * 修改时间：
 * 修改内容：
 * </pre>
 */
public class DatagramSocketUtil {

    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(DatagramSocketUtil.class);

    /**
     * 发送UDP请求
     * 
     * @param mac
     *            mac地址
     * @param host
     *            ip地址
     * @param port
     *            端口
     * @return 返回数据
     */
    public static void sendMac(String mac, String host, int port) {
        byte[] bytes = MacUtil.getDestBytes(mac);
        LOG.info("发送开机指令，MAC：{}，HOST ： {} ， PORT ： {}",mac, host, port);
        try {
            DatagramSocket client = new DatagramSocket();
            InetAddress addr = InetAddress.getByName(host);
            DatagramPacket sendPacket = new DatagramPacket(bytes, bytes.length, addr, port);
            client.send(sendPacket);
            client.close();
        } catch (Exception e) {
            LOG.error("发送UDP请求异常", e);
        }
    }

    /**
     * 发送UDP请求
     * 
     * @param bytes
     *            发送信息
     * @param host
     *            ip地址
     * @param port
     *            端口
     * @return 返回数据
     */
    public static String send(byte[] bytes, String host, int port) {
        LOG.info("发送UDP请求，HOST : {} , PORT : {}", host, port);
        try {
            DatagramSocket client = new DatagramSocket();
            InetAddress addr = InetAddress.getByName(host);
            DatagramPacket sendPacket = new DatagramPacket(bytes, bytes.length, addr, port);
            client.send(sendPacket);
            byte[] recvBuf = new byte[1024];
            DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
            client.receive(recvPacket);
            String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());
            client.close();
            LOG.info("接受返回值 : {}", recvStr);
            return recvStr;
        } catch (Exception e) {
            LOG.error("发送UDP请求异常", e);
            return null;
        }
    }

    /**
     * 发送UDP请求
     * 
     * @param msg
     *            发送信息
     * @param host
     *            ip地址
     * @param port
     *            端口
     * @return 返回数据
     */
    public static String send(String msg, String host, int port) {
        return send(msg.getBytes(), host, port);
    }

    public static void main(String[] args) {
        new Thread(
                () -> {
                    try {
                        DatagramSocket server = new DatagramSocket(8888);
                        byte[] recvBuf = new byte[1024];
                        DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
                        server.receive(recvPacket);
                        String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());
                        System.out.println(recvStr);
                        int port = recvPacket.getPort();
                        InetAddress addr = recvPacket.getAddress();
                        byte[] sendBuf;
                        sendBuf = recvStr.getBytes();
                        DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, port);
                        server.send(sendPacket);
                        server.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        ).start();

        String string = send("AAAAAAAAAAAAAAAAAAAAAAAA", "127.0.0.1", 8888);
        System.out.println(string);
    }
}
